import store from '@/store/index.js';
import {debounce} from '@/utils/tools.js'


//信息授权
export function location(isLoading) {
		let that = this
		
		if(isLoading){
			uni.showLoading({
				title: '位置获取中...',
				mask: true
			})
		}
		
		uni.getSetting({
			success: (res) => {
				// res.authSetting有多种属性，比如res.authSetting['scope.userInfo']表示获取用户头像等信息，详情看官方文档

				// res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
				// res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
				// res.authSetting['scope.userLocation'] == true    表示 地理位置授权
				// 拒绝授权后再次进入重新授权
				if (res.authSetting['scope.userLocation'] != undefined && res.authSetting[
						'scope.userLocation'] != true) {

					// console.log('authSetting:status:拒绝授权后再次进入重新授权', res.authSetting['scope.userLocation'])
					uni.showModal({
						title: '',
						content: '【xxx】需要获取你的地理位置，请确认授权',
						success: function(res) {

							if (res.cancel) {
								uni.showToast({
									title: '拒绝授权',
									icon: 'none'
								})
								// setTimeout(() => {
								//     uni.navigateBack()
								// }, 1500)
							} else if (res.confirm) {
								uni.openSetting({
									success: function(dataAu) {
										// console.log('dataAu:success', dataAu)
										if (dataAu.authSetting[
											"scope.userLocation"] == true) {
											//再次授权，调用uni.getLocation的API
											
												getLocation(dataAu)
											
										} else {
											uni.showToast({
												title: '授权失败',
												icon: 'none'
											})
											// setTimeout(() => {
											//     uni.navigateBack()
											// }, 1500)
										}
									}
								})
							}

						} //success
					})
				}
				// 初始化进入，未授权
				else if (res.authSetting['scope.userLocation'] == undefined) {
					// console.log('authSetting:status:初始化进入，未授权', res.authSetting['scope.userLocation'])
					//调用uni.getLocation的API
					getLocation(res)
				}
				// 已授权
				else if (res.authSetting['scope.userLocation']) {
					// console.log('authSetting:status:已授权', res.authSetting['scope.userLocation'])
					//调用uni.getLocation的API
					getLocation(res)
				}
			}
		})
	}


	// 微信获得经纬度
	function getLocation(userLocation) {
		let that = this
		uni.getLocation({
			type: "wgs84",
			success: function(res) {
				console.log('getLocation:success', res)
				let latitude = res.latitude
				let longitude = res.longitude
				console.log('latitude:' + latitude, 'longitude' + longitude);
				let location = latitude+'-'+longitude
				store.dispatch('getLocations',location)
				uni.hideLoading()
			},
			fail: function(res) {
				console.log('fail', res.errMsg);

				if (res.errMsg === 'getLocation:fail:auth denied') {
					uni.showToast({
						title: '拒绝授权',
						icon: 'none'
					})
					// setTimeout(() => {
					//     uni.navigateBack()
					// }, 1500)
					return
				}
				if (!userLocation || !userLocation.authSetting['scope.userLocation']) {
					// that.getUserLocation()
				} else if (userLocation.authSetting['scope.userLocation']) {
					uni.showModal({
						title: '',
						content: '请在系统设置中打开定位服务',
						showCancel: false,
						success: result => {
							if (result.confirm) {
								uni.navigateBack()
							}
						}
					})
				} else {
					uni.showToast({
						title: '授权失败',
						icon: 'none'
					})
					// setTimeout(() => {
					//     uni.navigateBack()
					// }, 1500)
				}
			}
		})
	}